package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

/**
 * @author krystal
 * @date 2023/3/7 11:36
 */
@Controller  //控制器
@ResponseBody  //返回的是数据，而非页面
@PropertySource(value = "application.properties",encoding = "utf-8")
public class TestController {

    @Value("${myname}")
    private String myname;

    @Value("${myname2}")
    private String myname2;

    @Value("${myString}")
    private String myString;

    @Value("${myString1}")
    private String myString1;

    @Value("${myString2}")
    private String myString2;

    @Autowired
    private Star star;

    @PostConstruct
    public void postConstruct(){
        System.out.println(star);
//        System.out.println("myString"+myString);
//        System.out.println("myString1"+myString1);
//        System.out.println("myString2"+myString2);
    }

    @RequestMapping("/hi")  //url路由注册
    public String sayHi(String name){
        //为空为null，默认值处理
        if (!StringUtils.hasLength(name)){
            name="baekhyun";
        }
        return "你好:"+name;
    }

    @RequestMapping("/getconfig")
    public String getConfig(){
        return "myname:"+ myname +"myname2:"+   myname2;
    }
}
